package com.personal.auto;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang.StringUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import com.common.util.Constants;

public class WeeklyServiceUpdate implements Constants {
	
    public void update() throws InterruptedException  {
    	
    	/*
		if (args.length != 1) {
          	System.out.println("Usage: java com.personal.auto.WeeklyServiceUpdate");
			System.exit(1);
		}
		*/
		
    	//Step 0: Start an Firefox Web Driver.
    	WebDriver driver = new FirefoxDriver();
        
        //Step 1: get property from online wiki.
    	driver.get(URL_ONLINE_WIKI);
    	Thread.sleep(SPACING_INTERVAL);
    	
    	driver.findElement(By.linkText("Login")).click();
    	Thread.sleep(SPACING_INTERVAL);
    	
        driver.findElement(By.name("u")).sendKeys(ACCOUNT_ONLINE_WIKI);
        driver.findElement(By.name("p")).sendKeys(PASSWORD_ONLINE_WIKI);
        List<WebElement> inputWEList = driver.findElements(By.tagName("input"));
        for (WebElement input : inputWEList) {
        	if("Login".equals(input.getAttribute("value"))) {
        		input.click();
        		break;
        	}
        }
        driver.get(PAGE_PHONE_SETTING);
        Thread.sleep(SPACING_INTERVAL * 2);
        
        WebElement preWE= driver.findElement(By.tagName("pre"));
        String[] allSettings = preWE.getText().split(" ");
        Map<String, String> settingMap = new HashMap<String, String>();
        for (String oneSetting : allSettings) {
        	String[] strArray = oneSetting.split("=");
        	if (strArray.length >= 2) {
        		settingMap.put(replaceBlank(strArray[0]), replaceBlank(strArray[1]));
        	}
        }
        
        //Step 2: Login to Service Website.
        driver.get(settingMap.get(URL_WEEKLY_SERVICE));
        Thread.sleep(SPACING_INTERVAL * 5);
        driver.findElement(By.name("username")).sendKeys(settingMap.get(ACCOUNT_WEEKLY_SERVICE));
        driver.findElement(By.name("password")).sendKeys(settingMap.get(PASSWORD_WEEKLY_SERVICE));
        List<WebElement> inputList = driver.findElements(By.tagName("input"));
        for (WebElement input : inputList) {
        	if("Sign In".equals(input.getAttribute("value"))) {
        		input.click();
        		break;
        	}
        }
        Thread.sleep(SPACING_INTERVAL * 2);
        
        List<WebElement> linkList = driver.findElements(By.name("a"));
        for (WebElement link : linkList) {
        	if (link.getText().endsWith("Time and Expenses")) {
        		link.click();
        		break;
        	}
        }
        Thread.sleep(SPACING_INTERVAL);
        
        WebElement liElement = driver.findElement(By.id("51224:809:-1:0"));
        liElement.findElement(By.tagName("a")).click();
        Thread.sleep(SPACING_INTERVAL);
        
        WebElement li2Element = driver.findElement(By.id("51224:809:73094:0"));
        li2Element.findElement(By.tagName("a")).click();
        Thread.sleep(SPACING_INTERVAL);
        
        WebElement li3Element = driver.findElement(By.id("TimeRecent Timecards"));
        li3Element.findElement(By.tagName("a")).click();
        Thread.sleep(SPACING_INTERVAL * 2);
        
        //Step 3: Start to create time card.
        if(!StringUtils.isBlank(settingMap.get(PERIOD_WEEKLY_SERVICE))) {
            WebElement periodSelect = driver.findElement(By.id("N57"));
            Select periodSelect1 = new Select(periodSelect);
            for (WebElement option : periodSelect1.getOptions()) {
            	if (option.getAttribute("value").startsWith(settingMap.get(PERIOD_WEEKLY_SERVICE))) {
            		periodSelect1.selectByValue(option.getAttribute("value"));
            		break;
            	}
            }
        }
        Thread.sleep(SPACING_INTERVAL);
        
        WebElement tempplateSelect = null;
        if(!StringUtils.isBlank(settingMap.get(TEMPLATE_WEEKLY_SERVICE))) {
        	String[] templateListStr = settingMap.get(TEMPLATE_WEEKLY_SERVICE).split(";");
        	
        	for (String templateStr : templateListStr) {
                applyTemplate(driver, tempplateSelect, templateStr);
        	}
        } else {
        	applyTemplate(driver, tempplateSelect, "DYNAMIC|SYS|LAST_TIMECARD");
        }

        WebElement buttonContinue = driver.findElement(By.id("review_uixr"));
        buttonContinue.click();
        Thread.sleep(SPACING_INTERVAL * 2);
        
        //WebElement buttonSubmit = driver.findElement(By.id("HxcSubmit_uixr"));
        //buttonSubmit.click();
        
        //driver.close();
    }
    
    
    private static String replaceBlank(String value) {
        String dest = "";
        if (value != null) {
            Pattern p = Pattern.compile("\\s*|\t|\r|\n");
            Matcher m = p.matcher(value);
            dest = m.replaceAll("");
        }
        return dest;
    }
    
    private static void applyTemplate(WebDriver driver, WebElement select, String templateValue) throws InterruptedException {
    	select = driver.findElement(By.id("TemplateList"));
        Select select1 = new Select(select);
        select1.selectByValue(templateValue);
        List<WebElement> buttons = driver.findElements(By.tagName("button"));
        for (WebElement button : buttons) {
        	if (button.getText().equals("Apply Template")) {
        		button.click();
        		break;
        	}
        }
        Thread.sleep(SPACING_INTERVAL * 2);
    	
    }
    
}